package community.hospital.util;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTHeader;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.JWTValidator;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static community.hospital.common.Constant.JWT_USER_ID;

/**
 * @Author：ydj
 * @Date：2023/1/21 15:31
 */
public class JwtUtil {
    private static final String KEY = "ydj";


    public static String getToken(Map<String,Object> payload) {
        return JWTUtil.createToken(payload,KEY.getBytes(StandardCharsets.UTF_8));
    }

    public static Object parseToken(String token,String key) {
        return JWTUtil.parseToken(token).getPayload(key);
    }

    public static boolean validateToken(String token) {

        return JWTUtil.verify(token, KEY.getBytes(StandardCharsets.UTF_8));
    }

    public static Long getUserId(String token) {
        Object parseToken = parseToken(token, JWT_USER_ID);
        return Long.valueOf(parseToken.toString());
    }

}
